package com.yyhh.helpcall.activity;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.yyhh.helpcall.R;
import com.yyhh.helpcall.activity.adapter.TestCallLogsAdapter;
import com.yyhh.helpcall.activity.adapter.TestRecordsAdapter;
import com.yyhh.helpcall.model.CallModel;
import com.yyhh.helpcall.model.RecordModel;
import com.yyhh.helpcall.utils.NetLog;
import com.yyhh.helpcall.utils.ScanCallLogUtils;
import com.yyhh.helpcall.utils.ScanRecordUtils;
import com.yyhh.helpcall.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TestActivity extends BaseActivity {

    private AppCompatButton callLogsBt, recordsBt,uploadBt;
    private RecyclerView recyclerView;
    private TestCallLogsAdapter testCallLogsAdapter;
    private TestRecordsAdapter testRecordsAdapter;
    private TextView messageTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        callLogsBt = findViewById(R.id.callLogsBt);
        recordsBt = findViewById(R.id.recordsBt);
        uploadBt = findViewById(R.id.uploadBt);
        messageTv = findViewById(R.id.message);

        recyclerView = findViewById(R.id.rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(linearLayoutManager);
        testCallLogsAdapter = new TestCallLogsAdapter();
        testRecordsAdapter = new TestRecordsAdapter();
        recyclerView.setAdapter(testCallLogsAdapter);

        callLogsBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testCallLogs();
            }
        });

        recordsBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testRecords();
            }
        });
        uploadBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<CallModel> callModels = ScanCallLogUtils.startScanCallLogs(TestActivity.this);

                ArrayList<RecordModel> recordModels = ScanRecordUtils.scanRecordFiles();

                long  last_timestamp = Long.parseLong("1600963200000");
                ArrayList<RecordModel> filterRecordModels = new ArrayList<>();
                //过滤出来匹配到的录音文件
                for (int i = 0; i < recordModels.size(); i++) {
                    RecordModel recordModel = recordModels.get(i);
                    if (recordModel.lastModifyTime > last_timestamp) {
                        NetLog.d("recordModel = " + recordModel.name);
                        filterRecordModels.add(recordModel);
                    }
                }

                Collections.sort(filterRecordModels,new Comparator<RecordModel>() {
                    @Override
                    public int compare(RecordModel o1, RecordModel o2) {
                        return (int) (o1.lastModifyTime - o2.lastModifyTime);
                    }
                });
                ArrayList<RecordModel> result = new ArrayList<>();

                matchingRecords(0,callModels,filterRecordModels,result);

                messageTv.setText("共扫描到了"+filterRecordModels.size()+"条录音文件，匹配到了"+ result.size() + "");

                recyclerView.setAdapter(testRecordsAdapter);
                testRecordsAdapter.datas.clear();
                testRecordsAdapter.datas.addAll(result);
                testRecordsAdapter.notifyDataSetChanged();
            }
        });
    }

    public void matchingRecords(int count,List<CallModel> callModels,List<RecordModel> recordModels,ArrayList<RecordModel> result){
        ArrayList<RecordModel> noResult = new ArrayList<>();
        for (int i = 0; i < recordModels.size(); i++) {
            RecordModel recordModel = recordModels.get(i);
            for (int j = 0;
                 j < callModels.size(); j++) {
                CallModel callModel = callModels.get(j);
                //callModel.duration 表示通话时长，时长大于0才匹配
                if (callModel.duration > 0
                        //录音文件的最后修改时间一定是晚于通话的开始时间，所以这里进行第一次判断
                        && recordModel.lastModifyTime - (callModel.date + callModel.duration * 1000) > 0
                        //录音文件的修改时间-通话的开始时间，差值再20s内，则匹配成功
                        //后边的count 为递归次数
                        && recordModel.lastModifyTime - (callModel.date + callModel.duration * 1000) <= (20 + count) * 1000) {
                    recordModel.targetCallModel = callModel;
                    //匹配到了，则添加到集合里
                    result.add(recordModel);
                    break;
                }
            }
            //没匹配到则额外处理
            if(result.indexOf(recordModel) == -1){
                noResult.add(recordModel);
                ToastUtils.show(recordModel.toString());
            }
        }

        //如果间隔太大则不匹配，如果递归次数超过3分钟则丢弃，肯定是通话记录被手动删除了
        if(count >= 60 * 5){
            return;
        }
        if(noResult.size() > 0){
            //说明没有匹配上,则一一直匹配下去，直到所有都匹配成功
            matchingRecords(count + 1,callModels,noResult,result);
        }
    }

    private void testCallLogs() {
        List<CallModel> callModels = ScanCallLogUtils.startScanCallLogs(this);
        messageTv.setText("共扫描到了"+callModels.size()+"条通话记录");
        recyclerView.setAdapter(testCallLogsAdapter);
        testCallLogsAdapter.datas.clear();
        testCallLogsAdapter.datas.addAll(callModels);
        testCallLogsAdapter.notifyDataSetChanged();
    }

    private void testRecords(){
        ArrayList<RecordModel> recordModels = ScanRecordUtils.scanRecordFiles();
        Collections.sort(recordModels,new Comparator<RecordModel>() {
            @Override
            public int compare(RecordModel o1, RecordModel o2) {
                return (int) (o1.lastModifyTime - o2.lastModifyTime);
            }
        });
        Collections.reverse(recordModels);
        messageTv.setText("共扫描到了"+recordModels.size()+"条录音文件");
        recyclerView.setAdapter(testRecordsAdapter);
        testRecordsAdapter.datas.clear();
        testRecordsAdapter.datas.addAll(recordModels);
        testRecordsAdapter.notifyDataSetChanged();
    }
}
